<?php
/**
 * 售后模块Api Service
 *
 * 售后模块Api Service的注意及说明
 * 以下use模块请不要互相伤害
 *
 * @author    xiecz <xiecz@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\Order;
use Log;
use App\Services\Tool\DataBase\SQL;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\Order\OrderTool;
use App\Services\Tool\Order\AfterTool;

class AfterApi{

    function __construct(OrderTool $orderTool,
                         AfterTool $afterTool){
        $this->orderTool = $orderTool;
        $this->afterTool = $afterTool;
    }

    /**
        售后列表
        参数：
            user_id         用户编号
            page            页码
            per_page        每页显示数据量
     */
    function user_order_aftermark($input){
        if(!isset($input['user_id']) ||
            !isset($input['page']) ||
            !isset($input['per_page']) ||
            !is_numeric($input['page']) ||
            !is_numeric($input['per_page']) ||
            $input['page'] <= 0||
            $input['per_page'] <= 0)
            return GeneralTool::createResult('','M00002');

        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');

        $input['per_page'] = empty($input['per_page']) ? 10 : ceil($input['per_page']);

        $afters = $this->afterTool->aftermark_lists($input);

        return GeneralTool::createResult($afters,'M00000');
    }

    /**
        申请售后
        参数：
            user_id         用户编号
            order_id        订单编号
            body            [
                                'order_detail_number'
                                'quantity'
                            ]
            message         描述
            images          [ 'image1':{'thumbnail':'','original_image':''},'image2','image3','image4','image5' ]
     */
    function submit_aftermark($input){
        if(!isset($input['user_id']) ||
            !isset($input['order_id']) ||
            !isset($input['body']) ||
            !isset($input['message']) ||
            !isset($input['images'])||
            GeneralTool::getcode($input['message']) == '')
            return GeneralTool::createResult('','M00002');

        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');
        //判断订单是否可申请售后
        $order = $this->orderTool->get_order_info_from_table_orders(['buyer_id'     => $input['user_id'],
                                                                     'order_id'     => $input['order_id'],
                                                                     'type'         => 'order',
                                                                     'is_cancel'    => 0,
                                                                     'is_aftermark' => 0,
                                                                     'state'        => 4]);
        if(!isset($order)) return GeneralTool::createResult('','M05026');
        $can_aftermark = $this->orderTool->order_can_aftermark($order);
        if(!$can_aftermark) return GeneralTool::createResult('','M05027');

        $body = json_decode($input['body'],true);
        $images = json_decode($input['images'],true);
        if(!is_array($body) || count($body) == 0) return GeneralTool::createResult('','M05028');
        if(!is_array($images) || count($images) <1 || count($images) > 5) return GeneralTool::createResult('','M05029');
        if(mb_strlen(trim($input['message'])) <= 0 && mb_strlen(trim($input['message'])) > 200) return GeneralTool::createResult('','M05030');

        $result = $this->afterTool->submit_aftermark($input);

        return GeneralTool::createResult('',$result);
    }

    /**
        物流公司列表
        参数：
            user_id     用户编号
     */
    function get_express_list($input){
        if(!isset($input['user_id'])) return GeneralTool::createResult('','M00002');
        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');
        $list = $this->orderTool->getExpressList();
        return GeneralTool::createResult($list,'M00000');
    }

    /**
        填写退货单号
        参数：
            user_id             用户编号
            order_after_id      售后单号
            express_id          物流公司编号
            order_express_id    物流单号
     */
    function submit_order_aftermark_express($input){
        if(!isset($input['user_id']) ||
            !isset($input['order_after_id']) ||
            !isset($input['express_id']) ||
            !isset($input['order_express_id']))
            return GeneralTool::createResult('','M00002');

        if($this->afterTool->get_order_aftermark_count(['order_after_id' => $input['order_after_id'],
                                                        'user_id'        => $input['user_id'],
                                                        'state'          => 2]) == 0)
            return GeneralTool::createResult('','M05036');
        if(!preg_match("/^[a-z0-9\#]*$/", $input['express_id']) ||
            GeneralTool::getcode($input['express_id']) == '')
            return GeneralTool::createResult('','M00004');
        if(!preg_match("/^[a-z0-9\#]*$/", $input['order_express_id']) ||
            GeneralTool::getcode($input['order_express_id']) == '')
            return GeneralTool::createResult('','M00004');
        $after = $this->afterTool->submit_order_aftermark_express($input);
        $arr = $after == 'M00000' ? $this->afterTool->get_aftermark_detail_from_page($input) : '';
        if($after == 'M00000') $arr->state_str = $arr->title;

        return GeneralTool::createResult($arr,$after);
    }

    /**
        查看售后单物流信息
        参数：
            order_after_id          售后单编号
            user_id                 用户编号
     */
    function order_aftermark_express_detail($input){
        if(!isset($input['order_after_id']) ||
            !isset($input['user_id']))
            return GeneralTool::createResult('','M00002');
        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');

        $order_express                    = $this->afterTool->get_aftermark_user_express($input);
        if(!isset($order_express->send_order_express_id)) return GeneralTool::createResult('','M05040');
        $order['order_express_id']        = isset($order_express->send_order_express_id)?$order_express->send_order_express_id:'';
        $order['express_name']            = isset($order_express->express_name)?$order_express->express_name:'';
        $order['express_id']              = isset($order_express->send_express_id)?$order_express->send_express_id:'';
        $order['good_total_quantity']     = $this->afterTool->get_order_aftermark_good_total_quantity($input['order_after_id']);
        $order['good_total_quantity_str'] = '共' . $order['good_total_quantity'] . '件商品';
        $images                           = $this->afterTool->get_order_aftermark_first_picture($input['order_after_id'],2);
        $order['thumbnail']               = isset($images['thumbnail']) ? $images['thumbnail'] : [];
        $order['original_image']          = isset($images['original_image']) ? $images['original_image'] : [];
        $order['express']                 = $this->orderTool->getExpress($order);

        return GeneralTool::createResult($order,'M00000');
    }

    /**
        售后单详情
        参数：
            user_id                 用户编号
            order_after_id          售后单编号
     */
    function get_aftermark_detail($input){
        if(!isset($input['order_after_id']) ||
            !isset($input['user_id']))
            return GeneralTool::createResult('','M00002');
        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');
        $result = $this->afterTool->get_aftermark_detail_from_page($input);
        if(!isset($result->order_after_id)) return GeneralTool::createResult('','M05040');
        return GeneralTool::createResult($result,'M00000');
    }

    /**
        售后单确认收货
        参数：
            user_id                 用户编号
            order_after_id          售后单编号
     */
    function order_aftermark_sign($input){
        if(!isset($input['order_after_id']) ||
            !isset($input['user_id']))
            return GeneralTool::createResult('','M00002');
        //判断用户是否存在
        if(!$this->orderTool->isUser($input['user_id'])) return GeneralTool::createResult('','M05001');

        $result = $this->afterTool->order_aftermark_sign($input);

        return $result;
    }

    function test($input){
        return GeneralTool::createResult('','M00000');
    }
}
